<?php


namespace app\home\severs;


use think\Exception;
use think\facade\Db;

class InventoryOrderSever extends Base
{
    public static function onSure($param,$uid=0)
    {
        $id=$param['id']??0;
        if (!$id){
            return self::jump(1,'参数缺失');
        }
        $mx=Db::connect('company')->name('inventory_order_detail')->where('status',1)->where('inventory_id',$id)->select();
        if (!$mx){
            return self::jump(1,'请添加明细');
        }
        Db::connect('company')->startTrans();
        try {
            Db::connect('company')->name('inventory_order')->where('id',$id)->update(['inventory_user'=>$uid,'status'=>2,'update_time'=>time(),'checkTime'=>time()]);
            Db::connect('company')->name('inventory_order_detail')->where('inventory_id',$id)->where('status',1)->update(['status'=>2,'isDo'=>1,'update_time'=>time()]);
            $params=request()->param();
            $params['id']=$id;
            add_log('check', $id, $params,'盘点单',$uid);
            Db::connect('company')->commit();
        }catch (\Exception $e){
            Db::connect('company')->rollback();
            return self::jump(1,'操作失败:'.$e->getMessage());
        }
        return self::jump(0,'操作成功');
}

    public static function panying($param,$uid=0)
    {
        $id=$param['id']??'';
        if (!$id){
            return self::jump(1,'参数缺失');
        }
        $info=Db::connect('company')->name('inventory_order')->where('id',$id)->find();
        if (!$info){
            return self::jump(1,'操作有误');
        }
        $paList=[];
        $mxList=Db::connect('company')->name('inventory_order_detail')
            ->alias('iod')
            ->join('material_kucun_view omi',"omi.mdId=iod.material_dossier_id and omi.wId= {$info['warehouse_id']}",'left')
            ->where('iod.status','>','-1')
            ->where('iod.is_pd','2')
            ->where('iod.inventory_id',$id)
            ->field('iod.*,omi.countNum as baseNum,omi.sendNum,omi.locator')
            ->select();

        foreach ($mxList as $item){
            $item['haveNum']=$item['baseNum']-$item['sendNum'];
            if ($item['backNum']<$item['num']){
                $item['num']=$item['num']-$item['backNum'];
                $paList[]=$item;
            }
        }
        if (empty($paList)){
            return self::jump(1,'没有可盘点的数据');
        }
        Db::connect('company')->startTrans();
//        $wCode=Db::connect('company')->name('warehouse')->where('id',$info['warehouse_id'])->value('wCode');
        $ordernum=dJBHBySql('in_receipt','orderNum','');
        try {
            $rukuData=[
                'type'=>'盘盈入库',
                'orderNum'=>$ordernum,
                'inventory_order_id'=>$id,
                'create_user'=>$uid,
                'incom_user'=>$uid,
                'incomTime'=>date('Y-m-d'),
                'checkTime'=>time(),
                'status'=>'2',
                'create_time'=>time(),
                'update_time'=>time(),
            ];

            $rukuId=Db::connect('company')->name('in_receipt')->insertGetId($rukuData);
            foreach ($paList as $v){
                if (empty($v)){
                    throw new Exception('没有可盘点的数据');
                }
                $rukuMx=[
                    'in_receipt_id'=>$rukuId,
                    'warehouse_id'=>$info['warehouse_id'],
                    'inventory_order_detail_id'=>$v['id'],
                    'locator'=>$v['locator'],
                    'num'=>$v['num'],
                    'status'=>'2',
                    'material_dossier_id'=>$v['material_dossier_id'],
                    'create_time'=>time(),
                    'update_time'=>time(),
                ];
                Db::connect('company')->name('in_receipt_detail')->insertGetId($rukuMx);
                Db::connect('company')->name('inventory_order_detail')->where('id',$v['id'])
                    ->update(['type'=>1,'is_pd'=>1,'pdNum'=>$v['num'],'pdTime'=>time()]);

            }
            add_log('pd', $id, $paList,'盘点单',$uid);
            Db::connect('company')->commit();
        }catch (\Exception $e){
            Db::connect('company')->rollback();
            return self::jump(1,'操作失败:'.$e->getMessage());
        }

        return self::jump(0,'操作成功');
        
}

    public static function pankui($param,$uid=0)
    {
        $id=$param['id']??'';
        if (!$id){
            return self::jump(1,'参数缺失');
        }
        $info=Db::connect('company')->name('inventory_order')->where('id',$id)->find();
        if (!$info){
            return self::jump(1,'操作有误');
        }
        $paList=[];
        $mxList=Db::connect('company')->name('inventory_order_detail')
            ->alias('iod')
            ->join('material_kucun_view omi',"omi.mdId=iod.material_dossier_id and omi.wId= {$info['warehouse_id']}",'left')
            ->where('iod.status','>','-1')
            ->where('iod.is_pd','2')
            ->where('iod.inventory_id',$id)
            ->field('iod.*,omi.countNum as baseNum,omi.sendNum,omi.locator,omi.id as incomId')
            ->select();
        foreach ($mxList as $item){
            $item['haveNum']=$item['baseNum']-$item['sendNum'];
            if ($item['backNum']>$item['num']){
                $item['num']=$item['backNum']-$item['num'];
                $paList[]=$item;
            }
        }
        if (empty($paList)){
            return self::jump(1,'没有可盘点的数据');
        }
        Db::connect('company')->startTrans();
//        $wCode=Db::connect('company')->name('warehouse')->where('id',$info['warehouse_id'])->value('wCode');
        $ordernum=dJBHBySql('out_receipt','orderNum','');
        try {
            $chukuData=[
                'type'=>'盘亏出库',
                'inventory_order_id'=>$id,
                'orderNum'=>$ordernum,
                'out_user'=>$uid,
                'create_user'=>$uid,
                'outTime'=>date('Y-m-d'),
                'checkTime'=>time(),
                'status'=>'2',
                'create_time'=>time(),
                'update_time'=>time(),
            ];

            $chukuId=Db::connect('company')->name('out_receipt')->insertGetId($chukuData);
            foreach ($paList as $v){
                if (empty($v)){
                    throw new Exception('没有可盘点的数据');
                }
                $chukuMx=[
                    'out_receipt_id'=>$chukuId,
//                    'incom_receipt_detail_id'=>$v['incomId'],
                    'inventory_order_detail_id'=>$v['id'],
                    'num'=>$v['num'],
                    "warehouse_id"=>$info['warehouse_id'],
//                    "locator"=>$info['warehouse_id'],
                    'status'=>'2',
                    'material_dossier_id'=>$v['material_dossier_id'],
                    'create_time'=>time(),
                    'update_time'=>time(),
                ];
                Db::connect('company')->name('out_receipt_detail')->insertGetId($chukuMx);
                Db::connect('company')->name('inventory_order_detail')->where('id',$v['id'])
                    ->update(['type'=>2,'is_pd'=>1,'pdNum'=>$v['num'],'pdTime'=>time()]);

            }
            add_log('pd', $id, $paList,'盘点单',$uid);
            Db::connect('company')->commit();
        }catch (\Exception $e){
            Db::connect('company')->rollback();
            return self::jump(1,'操作失败:'.$e->getMessage());
        }

        return self::jump(0,'操作成功');
}

    public static function onCancel($param,$uid=0)
    {
        $id=$param['id']??0;
        if (!$id){
            return self::jump(1,'参数缺失');
        }
        $info=Db::connect('company')->name('inventory_order')->where('id',$id)->find();
        if (!$info||$info['status']!=2){
            return self::jump(1,'参数有误');
        }
        $mx=Db::connect('company')->name('inventory_order_detail')->where('status',2)->where('inventory_id',$id)->select();
        if (!$mx){
            return self::jump(1,'请添加明细');
        }
        $ruku=Db::connect('company')->name('in_receipt')->where('inventory_order_id',$id)->where('status',2)->count();
        $chuku=Db::connect('company')->name('out_receipt')->where('inventory_order_id',$id)->where('status',2)->count();
        if ($ruku){
            return self::jump(1,'请撤销入库单后再撤销');
        }
        if($chuku){
            return self::jump(1,'请撤销出库单后再撤销');
        }
        $inData=[
            'status'=>1,
            'checkTime'=>time(),
            'inventory_user'=>0,
            'update_time'=>time()
        ];
        $detailData=[
            'status'=>1,
            'is_pd'=>2,
            'isDo'=>2,
            'update_time'=>time()
        ];
        Db::connect('company')->startTrans();
        try {
            Db::connect('company')->name('inventory_order')->where('id',$id)->update($inData);
            Db::connect('company')->name('inventory_order_detail')->where('status','>',-1)
                ->where('inventory_id',$id)->update($detailData);
            add_log('back', $id, $param,'盘点单',$uid);
            Db::connect('company')->commit();
        }catch (\Exception $e){
            Db::connect('company')->rollback();
            return self::jump(1,'操作失败:'.$e->getMessage());
        }
        return self::jump(0,'操作成功');

}
}